<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <comp-result-item
        v-for="(item, index) in list"
        :key="item.id + index + item.name"
      >
        <div class="resItem" @click="showDesc(item.id)">
          <img :src="item.pic" alt="" />
          <div>
            <h4>{{ item.name }}</h4>
            <div class="introd">
              <p class="num">准备时长:{{ item.preparetime }}</p>
              <p class="time">亨饪时长：{{ item.cookingtime }}</p>
            </div>
          </div>
        </div>
      </comp-result-item>
    </van-list>
  </div>
</template>

<script>
import CompResultItem from "@/components/comp-resultItem.vue";
export default {
  name: "PageList",
  components: { CompResultItem },
  props: {
    menulist: {
      type: Array,
    },
  },
  data() {
    return {
      list: this.menulist,
      loading: false,
      finished: false,
    };
  },
  methods: {
    onLoad() {
      this.page++;
      this.loading = true;
      let id = this.$route.query.id;
      let pro = axios.get(
        `/byclass?classid=${id}&start=0&num=10&appkey=d9c770661cb89052d8785cadce76e57a`
      );
      pro.then((response) => {
        if (response.data.code == 10000) {
          let res = response.data.result.result.list;
          if (res == undefined) {
            return false;
          }
          if (res.length == 0) {
            this.finished = true;
          }
          this.list = [...this.list, ...res];
          this.loading = false;
        }
      });
    },
    showDesc(id) {
      this.$router.push({
        path: "/detail",
        query: {
          id: id,
        },
      });
      localStorage.setItem("list", JSON.stringify(this.list));
    },
  },
};
</script>

<style lang="less" scoped>
.resItem {
  display: flex;
  margin-bottom: 20px;
  img {
    width: 100px;
  }
  > div {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 15px 0 0 15px;
    h4 {
      font-size: 18px;
    }
    p {
      font-size: 14px;
    }
  }
}
</style>